package com.lemon.ai.socket.packets.spiritgame.model.generator.utils;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import com.lemon.ai.socket.packets.spiritgame.model.dto.Column;
import com.lemon.ai.socket.packets.spiritgame.model.dto.Property;

public class StringUtils {

	private static Properties prop;
	static {
		try {
			prop = new Properties();
			prop.load(new FileInputStream(new File(
					"classNameDefine.properties")));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 将字符串的首字母大写，一般用于将表名转换为类名
	 * 
	 * @param tableName
	 *            表名
	 * @return 类名
	 */
	public static String generateClassName(String tableName) {

		if(prop.containsKey(tableName))
			return prop.getProperty(tableName);
		return xxx(tableName).replaceFirst(tableName.substring(0, 1), tableName.substring(0, 1).toUpperCase());

	}

	public static String xxx(String columnName) {
		String[] labels = columnName.split("_");
		String r = labels[0];
		for (int i = 1; i < labels.length; i++)
			r = r + generateClassName(labels[i]);
		return r;
	}
	
	/**
	 * 将第一个字符转换成大写
	 * @param src
	 * @return
	 */
	public static final String firstToUpperCase(String src) {
		return src.replaceFirst(src.substring(0, 1), src.substring(0, 1).toUpperCase());
	}

	/**
	 * 将类名转换为参数名，即第一个字母小写，如：User - user
	 * 
	 * @param tableName
	 *            表名
	 * @return 参数名
	 */
	public static String generateParameterName(String tableName) {

		String name = hump(tableName);
		
		if(name.length() > 7) {
			StringBuilder sb = new StringBuilder();

			char[] chars = name.toCharArray();

			for (int i = 0; i < chars.length; i++) {
				char c = chars[i];
				if(i == 0 || Character.isUpperCase(c)) {
					sb.append(Character.toLowerCase(c));
				}
			}
			return sb.toString();
		} else {
			return name;
		}
	}

	/**
	 * 将普通字符串, 包含下划线的, 转换成驼峰标识 的字符串
	 * @param srcText
	 * @return
	 */
	public static String hump(String srcText) {

		//把下划线之后的一个字符都变为大写
		StringBuilder sb = new StringBuilder();
		char[] chars = srcText.toCharArray();

		for (int i = 0; i < chars.length; i++) {
			if(i != 0 && chars[i - 1] == '_' ) {
				sb.append(Character.toUpperCase(chars[i]));
			} else {
				sb.append(Character.toLowerCase(chars[i]));
			}
		}

		String text = sb.toString().replace("_", "");
		return text;
	}

	/**
	 * 将字段转换为属性 
	 * 其中包括将字段名转换为属性名，将整型的字段类型转换为字符型的属性类型
	 * 如将Column(name = user_name; type = -1)转换为Property(name = userName; type = String)
	 * 
	 * @param column
	 *            字段
	 * @return Property
	 */
	public static Property generatePropertyFromColumn(Column column) {
		Property p = new Property();
		p.setName(hump(column.getName()));			//成员名
		p.setType(DbUtils.updateTypeFromDb(column.getType()) );	//成员类型
		return p;
	}

	public static void main(String[] args) {

		System.out.println(generateParameterName("aaa"));
		System.out.println(generateParameterName("activation_code"));
		System.out.println(generateParameterName("activation_code_xsklb"));
		System.out.println(generateParameterName("applyplace"));
		System.out.println(generateParameterName("asevrver_all_define"));
		System.out.println(generateParameterName("awards"));
		System.out.println(generateParameterName("base_data_staff_color"));
		System.out.println(generateParameterName("base_data_staff_skills_color"));
		System.out.println(generateParameterName("base_data_staff_talent_color"));
		System.out.println(generateParameterName("base_line_im"));
		System.out.println(generateParameterName("basic_apply_list"));
		System.out.println(generateParameterName("basic_commerce_message"));
		System.out.println(generateParameterName("basic_commerce_reltionship"));
		System.out.println(generateParameterName("basic_commerceunion"));
		System.out.println(generateParameterName("basic_member"));
		System.out.println(generateParameterName("building_data_agency"));
		System.out.println(generateParameterName("building_data_base"));
		System.out.println(generateParameterName("building_data_company"));
		System.out.println(generateParameterName("building_data_home"));
		System.out.println(generateParameterName("building_data_store"));
		System.out.println(generateParameterName("building_data_store_d_log"));
		System.out.println(generateParameterName("city_elite"));
		System.out.println(generateParameterName("commerce_union"));
		System.out.println(generateParameterName("commerce_union_reltionship"));
		System.out.println(generateParameterName("commerce_union_user_info"));
		System.out.println(generateParameterName("coupons_draw_mark"));
		System.out.println(generateParameterName("cross_mission"));
		System.out.println(generateParameterName("data_base_platform_recharge"));
		System.out.println(generateParameterName("elite_equipment"));
		System.out.println(generateParameterName("elite_medal"));
		System.out.println(generateParameterName("elite_plant_tree"));
		System.out.println(generateParameterName("elite_skill"));
		System.out.println(generateParameterName("elite_special_educate"));
		System.out.println(generateParameterName("elite_staff"));
		System.out.println(generateParameterName("game_data_code_id"));
		System.out.println(generateParameterName("game_data_code_user"));
		System.out.println(generateParameterName("game_data_players_home_flowers"));
		System.out.println(generateParameterName("game_data_players_home_visit"));
		System.out.println(generateParameterName("game_data_thread_buffer"));
		System.out.println(generateParameterName("game_data_thread_useprop_his"));
		System.out.println(generateParameterName("game_data_user_man_log"));
		System.out.println(generateParameterName("game_data_user_prop_use"));
		System.out.println(generateParameterName("legendcommerce"));
		System.out.println(generateParameterName("objective"));
		System.out.println(generateParameterName("present_draw_mark"));
		System.out.println(generateParameterName("secretary_data_base"));
		System.out.println(generateParameterName("secretary_data_chronicles"));
		System.out.println(generateParameterName("staff_data_archives"));
		System.out.println(generateParameterName("staff_data_base"));
		System.out.println(generateParameterName("t_test"));
		System.out.println(generateParameterName("t_user"));
		System.out.println(generateParameterName("t_user_sessionid"));
		System.out.println(generateParameterName("user_data_ach"));
		System.out.println(generateParameterName("user_data_bag"));
		System.out.println(generateParameterName("user_data_bag1"));
		System.out.println(generateParameterName("user_data_bank_log"));
		System.out.println(generateParameterName("user_data_base"));
		System.out.println(generateParameterName("user_data_dinner"));
		System.out.println(generateParameterName("user_data_eat_dinner"));
		System.out.println(generateParameterName("user_data_eat_dinner_num"));
		System.out.println(generateParameterName("user_data_eat_lunch"));
		System.out.println(generateParameterName("user_data_eat_lunch_num"));
		System.out.println(generateParameterName("user_data_email"));
		System.out.println(generateParameterName("user_data_friends"));
		System.out.println(generateParameterName("user_data_grid"));
		System.out.println(generateParameterName("user_data_lunch"));
		System.out.println(generateParameterName("user_data_manager"));
		System.out.println(generateParameterName("user_data_offline_request"));
		System.out.println(generateParameterName("user_data_staff_communication"));
		System.out.println(generateParameterName("user_data_task"));
		System.out.println(generateParameterName("user_data_task_count"));
		System.out.println(generateParameterName("user_data_task_success"));
		System.out.println(generateParameterName("user_data_task_today"));
		System.out.println(generateParameterName("user_data_turn_num"));
	}
}
